﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyBanHang.GUI;
using QuanLyBanHang.Lib;

namespace QuanLyBanHang.GUI
{
    public partial class frmQuantriUser : Form
    {
        public frmQuantriUser()
        {
            InitializeComponent();
        }

        private void frmQuantriUser_Load(object sender, EventArgs e)
        {
             LoaddtgKhachHang();
        }
          private DataTable dtpKhachHang = KetNoi.GetTable("select * from tblNhanVien");
        public int i = 0;

      

        public void LoaddtgKhachHang()
        {
            dtgUser.DataSource = KetNoi.GetTable("select*from tblNhanVien");
            dtgUser.AllowUserToAddRows = false;
            dtgUser.AllowUserToDeleteRows = false;
            dtgUser.Columns["MaNhanVien"].HeaderText = "Mã nhân viên";
            dtgUser.Columns["MaNhanVien"].Width = 100;
            dtgUser.Columns["HoTen"].HeaderText="Tên họ tên";
            dtgUser.Columns["HoTen"].Width = 250;
            dtgUser.Columns["MatKhau"].HeaderText = "Mật khẩu";
            dtgUser.Columns["CapDo"].HeaderText = "Cấp độ";
            dtgUser.Columns["GhiChu"].HeaderText = "Ghi Chú";
            Lock();
        }

        private void dtgKhachHang_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaNV.Enabled = false;
            if (e.RowIndex >= 0)
            {
                txtMaNV.Text = dtpKhachHang.Rows[e.RowIndex]["MaNhanVien"].ToString();
                txtHoTen.Text = dtpKhachHang.Rows[e.RowIndex]["HoTen"].ToString();
                txtMatKhau.Text = dtpKhachHang.Rows[e.RowIndex]["MatKhau"].ToString();
                txtCapDo.Text = dtpKhachHang.Rows[e.RowIndex]["CapDo"].ToString();
                txtGhiChu.Text = dtpKhachHang.Rows[e.RowIndex]["GhiChu"].ToString();
            }
        }
        public void Lock()
        {
            txtMaNV.Enabled = false;
            txtHoTen.Enabled = false;
            txtMatKhau.Enabled = false;
            txtCapDo.Enabled = false;
            txtGhiChu.Enabled = false;
            btnIn.Enabled = true;
            btnIn.Enabled = false;
            btnXoa.Enabled = true;
            btnThoat.Enabled = true;
            btnSua.Enabled = true;
            btnThoat.Enabled = true;
        }

        public void unlock()
        {
            txtMaNV.Enabled = true;
            txtHoTen.Enabled = true;
            txtMatKhau.Enabled = true;
            txtCapDo.Enabled = true;
            txtGhiChu.Enabled = true;
            btnIn.Enabled = false;
            btnIn.Enabled = true;
            btnXoa.Enabled = false;
            btnThoat.Enabled = true;
            btnSua.Enabled = false;
            btnThoat.Enabled = false;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtMaNV.Enabled == true)
            {
                DataRow newr = dtpKhachHang.NewRow();
                newr["MaNhanVien"] = txtMaNV.Text;
                newr["HoTen"] = txtHoTen.Text;
                newr["MatKhau"] = txtMatKhau.Text;
                newr["GhiChu"] = txtGhiChu.Text;
                newr["CapDo"] = txtCapDo.Text;
                i = KetNoi.ExcuteSQL("insert into tblNhanVien values('" + newr["MaNhanVien"] + "',N'"
                                                                         + newr["HoTen"] + "',N'"
                                                                         + newr["MatKhau"] + "',N'"
                                                                         + newr["CapDo"] + "',N'"
                                                                         + newr["GhiChu"] + "')");
                if (i > 0)
                {
                    MessageBox.Show("Lưu thành công");
                }
                else
                {
                    MessageBox.Show("Lưu thất bại");
                }
                LoaddtgKhachHang();
            }
            else
            {
                DataRow newr = dtpKhachHang.NewRow();
                newr["MaNhanVien"] = txtMaNV.Text;
                newr["HoTen"] = txtHoTen.Text;
                newr["MatKhau"] = txtMatKhau.Text;
                newr["CapDo"] = txtCapDo.Text;
                newr["GhiChu"] = txtGhiChu.Text;
                i = KetNoi.ExcuteSQL("update tblNhanVien set HoTen=N'" + newr["HoTen"]
                                                           + "',MatKhau=N'" + newr["MatKhau"] 
                                                           + "',CapDo=N'" + newr["CapDo"] 
                                                           + "',GhiChu=N'" + newr["GhiChu"] 
                                                           + "'where MaNhanVien='" + newr["MaNhanVien"] + "'");
                if (i > 0)
                {
                    MessageBox.Show("Lưu thành công");
                }
                else
                {
                    MessageBox.Show("Lưu thất bại");
                }
                LoaddtgKhachHang();
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            txtMaNV.Text = "";
            txtHoTen.Text = "";
            txtMatKhau.Text = "";
            txtGhiChu.Text = "";
            txtCapDo.Text = "";
            unlock();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            unlock();
            txtMaNV.Enabled = false;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {

        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            dtgUser.DataSource = KetNoi.GetTable("select * from tblNhanVien where MaNhanVien like'" + '%' + txtTimKiem.Text + "%" + "' or HoTen like'" + '%' + txtTimKiem.Text + '%' + "'");
            dtgUser.AllowUserToAddRows = false;
            dtgUser.AllowUserToDeleteRows = false;
            dtgUser.Columns["MaNhanVien"].HeaderText = "Mã nhân viên";
            dtgUser.Columns["MaNhanVien"].Width = 100;
            dtgUser.Columns["HoTen"].HeaderText = "Họ tên";
            dtgUser.Columns["HoTen"].Width = 250;
            dtgUser.Columns["MatKhau"].HeaderText = "Mật khẩu";
            dtgUser.Columns["CapDo"].HeaderText = "Cấp độ";
            dtgUser.Columns["GhiChu"].HeaderText = "Ghi Chú";
            Lock();
        }

        private void dtgKhachHang_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            for (byte j = 0; j <dtgUser.Rows.Count; j++)
            {
                dtgUser.Rows[j].DefaultCellStyle.BackColor = Color.Honeydew;
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
    
}
